/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.core.models.cspec;

import java.io.StringReader;
import java.util.List;
import org.xml.sax.InputSource;
import org.zoundry.gaedt.core.models.ZGaeModel;
import test.org.zoundry.gaedt.core.ZGaeCoreTestCase;


/**
 * Tests the cspec reader.
 */
public class ZGaeCSpecReaderTest extends ZGaeCoreTestCase
{
    public void testCSpecReader() throws Exception
    {
        String xml =
            "<cspec:controllers xmlns:cspec=\"urn:com:zoundry:gae:core:controllerSpec:2008:07\" debug=\"true\">\r\n" +
            "   <cspec:controller path=\"/cOne\" class=\"com.zoundry.controllers.Controller1\" />\r\n" +
            "   <cspec:controller path=\"/cTwo\" class=\"com.zoundry.controllers.Controller2\" />\r\n" +
            "   <cspec:controller pattern=\"/*.html\" class=\"com.zoundry.controllers.HtmlController\" />\r\n" +
            "</cspec:controllers>";

        ZGaeCSpecReader reader = ZGaeCSpecReader.getInstance();
        ZGaeModel model = reader.read(new InputSource(new StringReader(xml)));
        assertEquals(ZGaeControllersModel.class, model.getClass());
        ZGaeControllersModel controllersModel = (ZGaeControllersModel) model;
        List<ZGaeControllerModel> controllers = controllersModel.getControllers();
        assertEquals(3, controllers.size());
        ZGaeControllerModel controller1 = controllers.get(0);
        assertNull("Pattern should have been null on controller 1.", controller1.getPattern());
        assertEquals("/cOne", controller1.getPath());
        assertEquals("com.zoundry.controllers.Controller1", controller1.getClassname());
        ZGaeControllerModel controller2 = controllers.get(1);
        assertNull("Pattern should have been null on controller 2.", controller2.getPattern());
        assertEquals("/cTwo", controller2.getPath());
        assertEquals("com.zoundry.controllers.Controller2", controller2.getClassname());
        ZGaeControllerModel controller3 = controllers.get(2);
        assertNull("Path should have been null on controller 3.", controller3.getPath());
        assertEquals("/*.html", controller3.getPattern());
        assertEquals("com.zoundry.controllers.HtmlController", controller3.getClassname());
    }
}
